package com.pucong.gitee.database.backup.business.sys.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pucong.gitee.database.backup.business.sys.mapper.SysRoleMapper;
import com.pucong.gitee.database.backup.business.sys.mapper.SysRoleResourceMapper;
import com.pucong.gitee.database.backup.business.sys.mapper.SysRoleUserMapper;
import com.pucong.gitee.database.backup.business.sys.vo.AddSysRoleResourceVo;
import com.pucong.gitee.database.backup.common.entity.sys.SysRole;
import com.pucong.gitee.database.backup.common.entity.sys.SysRoleResource;
import com.pucong.gitee.database.backup.common.entity.sys.SysRoleUser;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.utils.UUID;
import com.pucong.gitee.database.backup.utils.UserRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 用户基础信息 service实现类
 *
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-01
 */
@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysUserInfoService sysUserInfoService;

    @Override
    public List<SysRole> getSysRoleInfoList(SysRole sysRole) {
        List<String> userId = sysUserInfoService.selectChildrenUserId();
        if(userId.size() == 0){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysRole> params = new LambdaQueryWrapper<>();
        params.in(userId != null, SysRole::getCreateBy, userId);
        List<SysRole> sysRoles = sysRoleMapper.selectList(params);
        return sysRoles;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertSysRoleInfo(SysRole sysRole){
        UserJwt loginUser = UserRequest.getLoginUser();
        String uuid = UUID.uuid();
        sysRole.setUuid(uuid);
        sysRole.setCreateBy(loginUser.getUuid());
        sysRole.setCreateTime(new Date());
        return sysRoleMapper.insert(sysRole);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int updateSysRoleInfoById(String id, SysRole sysRole){
        SysRole sysRoleUpdate = new SysRole();
        sysRoleUpdate.setUuid(id);
        sysRoleUpdate.setRoleName(sysRole.getRoleName());
        sysRoleUpdate.setDescription(sysRole.getDescription());
        return sysRoleMapper.updateById(sysRoleUpdate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteSysRoleInfoById(String id){
        int i = sysRoleMapper.deleteById(id);
        // 删除角色资源
        LambdaQueryWrapper<SysRoleResource> sysRoleResourceParams = new LambdaQueryWrapper<>();
        sysRoleResourceParams.eq(SysRoleResource::getRoleId, id);
        sysRoleResourceMapper.delete(sysRoleResourceParams);
        // 删除角色用户
        LambdaQueryWrapper<SysRoleUser> sysRoleUserParams = new LambdaQueryWrapper<>();
        sysRoleUserParams.eq(SysRoleUser::getRoleId, id);
        sysRoleUserMapper.delete(sysRoleUserParams);
        return i;
    }

    @Override
    public int addSysRoleResource(String id, AddSysRoleResourceVo resourceIds) {
        UserJwt loginUser = UserRequest.getLoginUser();
        SysRoleResource sysRoleResource = new SysRoleResource();
        String uuid = UUID.uuid();
        sysRoleResource.setUuid(uuid);
        sysRoleResource.setResourceId(resourceIds.getResourceId());
        sysRoleResource.setResourceType(resourceIds.getResourceType());
        sysRoleResource.setCreateBy(loginUser.getUuid());
        sysRoleResource.setCreateTime(new Date());
        sysRoleResourceMapper.insert(sysRoleResource);
        return 1;
    }

    @Override
    public int deleteSysRoleResource(String id) {
        int delete = sysRoleResourceMapper.deleteById(id);
        return delete;
    }

    @Override
    public int addSysRoleUser(String id, String userId) {
        UserJwt loginUser = UserRequest.getLoginUser();
        SysRoleUser sysRoleUser = new SysRoleUser();
        String uuid = UUID.uuid();
        sysRoleUser.setUuid(uuid);
        sysRoleUser.setUserId(userId);
        sysRoleUser.setCreateBy(loginUser.getUuid());
        sysRoleUser.setCreateTime(new Date());
        sysRoleUserMapper.insert(sysRoleUser);
        return 1;
    }

    @Override
    public int deleteSysRoleUser(String id) {
        int delete = sysRoleResourceMapper.deleteById(id);
        return delete;
    }
}
